package impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import util.DBUtil;
import dao.lmpl.PlansDao;
import entity.Plans;
import entity.User;

public class PlansDaoImol implements PlansDao {

	@Override
	public int addPlan(Plans a) throws SQLException {
		String sql="insert into plans values (null,?,?,now(),?,?,?,0)";
		Connection conn=DBUtil.getConn();
		int num=0;
		PreparedStatement pt=conn.prepareStatement(sql);
		pt.setInt(1, a.getUserId());
		pt.setString(2,a.getTitle());
		pt.setInt(3,a.getEndDays());
		pt.setString(4,a.getMotivation());
		pt.setInt(5,a.getIcon());
		num=pt.executeUpdate();
		
		
			if(num>0){
				System.out.println("��ӳɹ�");
			}else{
				System.out.println("���ʧ��");
			}
		return num;
	}

	public List<Plans> find(int userid) throws SQLException {
			String sql="select * from plans where userid=? ";
			List list=new ArrayList();
	        Connection conn=util.DBUtil.getConn();
	        PreparedStatement pst=conn.prepareStatement(sql);
	        pst.setInt(1,userid);
	        ResultSet res=pst.executeQuery();
			while(res.next()){
				int planId=res.getInt(1);
				int userId=res.getInt(2);
				String title=res.getString(3);
				Date beginTime=res.getDate(4);
				int endDays=res.getInt(5);
				String motivation=res.getString(6);
				int icon=res.getInt(7);
				int status=res.getInt(8);
				Plans plans=new Plans(planId,userId,title,beginTime,endDays,motivation,icon,status);
				 list.add(plans);
				 
			}
			conn.close();
		return list;
	}

	@Override
	public User login(String user, int pass) throws SQLException {
		String sql="select * from admin where user=? and pass=? ";
		List list=new ArrayList();
        Connection conn=util.DBUtil.getConn();
        PreparedStatement pet=conn.prepareStatement(sql);
        pet.setString(1, user);
        pet.setInt(2, pass);
        ResultSet rs=pet.executeQuery();
        while(rs.next()){
        	int id=rs.getInt(1);
        	String us=rs.getString(2);
        	int pa=rs.getInt(3);
        	User User=new User(id, user, pass);
        	list.add(User);
        }
        
		return (User) list;
	}

	@Override
	public int clock(int userId, int plandId) throws SQLException {
		
		String sql="insert into planitem values (?,?) ";
		Connection conn=DBUtil.getConn();
		return 0;
	}
	public static void main(String[] args) {
		
	}
	
		
		
	}


